package com.google.android.apps.mytracks.io.docs;

import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;

import android.content.Context;
import android.database.Cursor;
import android.location.Location;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;


public class LinestringUtils {

  private static int MAX_POINTS_PER_UPLOAD = 30000;
  

  public static List<Location> generateLocations(Track track, Context context){
    //
    
  //Progress status
    int totalLocationsRead;
    int totalLocationsPrepared;
    int totalLocationsUploaded;
    int totalLocations;
    int totalSegmentsUploaded;
    
    
    MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(context);

    Cursor locationsCursor = providerUtils.getLocationsCursor(track.getId(), 0, -1, false);

    List<Location> locations = new ArrayList<Location>(MAX_POINTS_PER_UPLOAD);


//_------------------
    try {
      if (locationsCursor == null || !locationsCursor.moveToFirst()) {
        //Log.w(Constants.TAG, "Unable to get any points to upload");
        //return false;
      }

      totalLocationsRead = 0;
      //  totalLocationsPrepared = 0;
      //  totalLocationsUploaded = 0;
      totalLocations = locationsCursor.getCount();
  
      Location lastLocation = null;
      do {

        Location loc = providerUtils.createLocation(locationsCursor);
        locations.add(loc);

        lastLocation = loc;

        totalLocationsRead++;
      } while (locationsCursor.moveToNext());
  
    } 
    finally {
      if (locationsCursor != null) {
        locationsCursor.close();
      }
    }
    return locations;
  }
  
  /**
   * Appends the given location to the string in the format:
   * longitude,latitude[,altitude]
   *
   * @param location the location to be added
   * @param builder the string builder to use
   */
  private static void appendCoordinate(Location location, StringBuilder builder) {
    
    if(location.getLatitude() == 100.0){
      Log.i(Constants.TAG, "ERROR LatLng: " + Double.toString(location.getLongitude()));
      
    }else{
      //Log.i(Constants.TAG, Double.toString(location.getLatitude()));
      builder
        .append(location.getLongitude())
        .append(",")
        .append(location.getLatitude());
    }
    if (location.hasAltitude()) {
      builder.append(",");
      builder.append(location.getAltitude());
    }
  }

  /**
   * Returns a KML LineString.
   * @param locations 
   *
   * @param track the track.
   * @return the KML LineString.
   */
  public static String getKmlLineString(Track track, Context context) {
    List<Location> locations = generateLocations(track, context);
    
    StringBuilder builder = new StringBuilder("<LineString><coordinates>");
    for (Location location : locations) {
      appendCoordinate(location, builder);
      builder.append(' ');
    }
    builder.append("</coordinates></LineString>");
    return builder.toString();
  }
  
}
